﻿// SilverlightBridge
// Copyright (c) 2009
// by Open Light Group
// http://openlightgroup.net/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Configuration;
using System.Configuration;
using SilverlightFileManager;

public partial class _Default : System.Web.UI.Page
{
    string strCurrentVersion = "01.10.00";
    bool boolSetup = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        boolSetup = DatabaseReady();

        if (boolSetup)
        {
            if (User.Identity.IsAuthenticated)
            {
                lnkLogin.Visible = false;
                lnkLogOut.Visible = true;

                // Possibly show Administration link
                if (User.Identity.IsAuthenticated && (User.Identity.AuthenticationType == "Forms"))
                {
                    FormsIdentity id = (FormsIdentity)User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;
                    lnkAdministration.Visible = (ticket.UserData.ToString() == "Administrators");
                }

            }
            else
            {
                lnkLogin.Visible = true;
                lnkLogOut.Visible = false;
            }
        }
        else
        {
            lnkSetup.Visible = true;
        }
    }

    #region DatabaseReady
    private bool DatabaseReady()
    {
        // This method returns true if the database exists and the table is created
        bool CanConnect = true;

        try
        {
            SilverlightFileManagerDALDataContext SilverlightFileManagerDALDataContext = new SilverlightFileManagerDALDataContext();
            var result = from VersionNumber in SilverlightFileManagerDALDataContext.Versions
                         select VersionNumber;
            CanConnect = (result.FirstOrDefault().VersionNumber == strCurrentVersion) ? true : false;
        }
        catch (Exception e)
        {
            string strError = e.Message;
            CanConnect = false;
        }

        return CanConnect;
    }
    #endregion

    #region LogOut
    protected void lnkLogOut_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("Default.aspx");
    }
    #endregion
}
